package loginDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bookDao.BookDbconnection;
import table.Login;

public class LoginImpl implements LoginDao{ 
	String sql = null;
	int result = 0;
	@Override
	public List<Login> findAll() {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Login> LoginList = new ArrayList<Login>();
		try { 
			con = BookDbconnection.getConnection();
			sql = "SELECT login_id,login_name,login_password FROM login";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int id = rs.getInt(1);
				String loginName = rs.getString(2);
				String loginPassword = rs.getString(3);		
				Login login = new Login(id,loginName,loginPassword);
				LoginList.add(login);			
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs,ps);
		}
		return LoginList;
	}


	@Override
	public void delete(String x) {
		Connection con = null;
		PreparedStatement ps = null; 
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "DELETE FROM login WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			result = ps.executeUpdate();
			System.out.println("操作结果是" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		
	}

	@Override
	public void update(Login x) {
		// TODO Auto-generated method stub
		
	}

	/**
	 *检查是否具有对应的密码
	 */
	@Override
	public String check(String x) {
		String loginPassword = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try { 
			con = BookDbconnection.getConnection();
			sql = "SELECT login_password FROM login WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			rs = ps.executeQuery();
			while(rs.next()) {
				 loginPassword = rs.getString(1);				
			}
			//找不到匹配的密码
			if(loginPassword==null) {
				return "0";
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs,ps);
		}
		return loginPassword;
	}

	@Override
	public String findIdentity(String x) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String userIdentity = null;
		try { 
			con = BookDbconnection.getConnection();
			sql = "SELECT user_identity FROM user WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			rs = ps.executeQuery();
			while(rs.next()) {
				 userIdentity = rs.getString(1);				
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs,ps);
		}
		return userIdentity;
	}


	@Override
	public void save(String x, String y) {
		Connection con = null;
		PreparedStatement ps = null;
		PreparedStatement ps1 = null;
		ResultSet rs = null; 
		ResultSet rs1 = null;
		String sql1 = null;
		int result1 = 0;
		try {
			con = BookDbconnection.getConnection();
			sql = "INSERT INTO user VALUE(null,null,null,?,null,?,?)";
			sql1 = "INSERT INTO login VALUE(null,?,?)";
			ps = con.prepareStatement(sql);
			ps1 = con.prepareStatement(sql1);			
			ps.setString(1,y);//密码
			ps.setString(2,x);//用户名
			ps.setString(3, "读者");
			ps1.setString(1,x);
			ps1.setString(2,y);
			result = ps.executeUpdate();
			result1 = ps1.executeUpdate();
			System.out.println("操作结果是"+result);
			System.out.println("操作结果是"+result1);
		}catch(SQLException e) {
			e.printStackTrace();
		}finally{
			BookDbconnection.close(rs, ps);
			BookDbconnection.close(rs1, ps1);
		}
		
	}
	
}
